package com.eightworld3.supermarket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eightworld3.supermarket.domain.WareType;
import com.eightworld3.supermarket.mapper.WareTypeMapper;
import com.eightworld3.supermarket.service.AssetsEnum;
import com.eightworld3.supermarket.service.WareTypeService;
import com.eightworld3.supermarket.service.dto.WareTypeDTO;
import com.eightworld3.supermarket.service.dto.add.WareTypeAddDTO;
import com.eightworld3.supermarket.service.dto.result.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author EightWorld3
 */
@Service
public class WareTypeServiceImpl extends ServiceImpl<WareTypeMapper,WareType> implements WareTypeService {
    private final WareTypeMapper wareTypeMapper;

    /**
     * WareTypeServiceImpl 构造方法
     * @param wareTypeMapper 用户类型Mapper
     */
    public WareTypeServiceImpl(WareTypeMapper wareTypeMapper){
        this.wareTypeMapper = wareTypeMapper;
    }

    /**
     * 查询所有
     * @return 返回所有WareType(商品类型)
     */
    @Override
    public List<WareType> selectAll(){
        return wareTypeMapper.selectList(null);
    }

    /**
     * 根据wareTypeId查询WareType
     * @param wareTypeId ID号
     * @return 返回wareTypeId对应的WareType对象(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult selectByWareTypeId(Integer wareTypeId){
        if (wareTypeId == null) {
            return new ResponseResult(2, "null");
        }

        WareType entity = wareTypeMapper.selectById(wareTypeId);
        if (entity == null) {
            return new ResponseResult(2, "没有该对象");
        }

        return new ResponseResult(1,"成功",entity);
    }

    /**
     * 新增WareType
     * @param wareTypeAddDTO WareType新增DTO对象
     * @return 返回当前当次新增状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult addWareType(WareTypeAddDTO wareTypeAddDTO){
        WareType entity = new WareType();

        // 类型名称字符大小限制
        if (wareTypeAddDTO.getWareTypeName().getBytes().length > AssetsEnum.NAME_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "类型名称过长，字符大小需 <= 32Byte");
        }else {
            entity.setWareTypeName(wareTypeAddDTO.getWareTypeName());
        }

        // 备注内容字符限制
        if(wareTypeAddDTO.getWareTypeRemark().getBytes().length > AssetsEnum.REMARK_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "备注内容过多，需 <= 120Byte");
        }else {
            entity.setWareTypeRemark(wareTypeAddDTO.getWareTypeRemark());
        }

        return new ResponseResult(1,"成功",wareTypeMapper.insert(entity));
    }

    /**
     * 修改WareType
     * @param wareTypeDTO WareType DTO对象
     * @return 返回当前当次更新状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult updateWareType(WareTypeDTO wareTypeDTO){
        WareType entity = new WareType();

        // 修改对象存在判断
        WareType existEntity = wareTypeMapper.selectById(wareTypeDTO.getWareTypeId());
        if (existEntity == null){
            return new ResponseResult(2, "商品价格对象不存在");
        }else {
            entity.setWareTypeId(wareTypeDTO.getWareTypeId());
        }

        // 类型名称字符大小限制
        if (wareTypeDTO.getWareTypeName().getBytes().length > AssetsEnum.NAME_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "类型名称过长，字符大小需 <= 32Byte");
        }else {
            entity.setWareTypeName(wareTypeDTO.getWareTypeName());
        }

        // 备注内容字符限制
        if(wareTypeDTO.getWareTypeRemark().getBytes().length > AssetsEnum.REMARK_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "备注内容过多，需 <= 120Byte");
        }else {
            entity.setWareTypeRemark(wareTypeDTO.getWareTypeRemark());
        }

        return new ResponseResult(1,"成功",wareTypeMapper.updateById(entity));
    }

    /**
     * 删除WareType
     * @param wareTypeId ID号
     * @return 返回当前当次删除状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult deleteWareTypeByWareTypeId(int wareTypeId){
        int deleteNum = wareTypeMapper.deleteById(wareTypeId);
        if (deleteNum == 0) {
            return new ResponseResult(0, "对象不存在");
        }
        return new ResponseResult(1, "成功");
    }
}
